package main

import (
	"context"
	"fmt"
	"github.com/olivere/elastic/v7"
)

// 基于索引的属性，创建结构体
// "id": { "type": "long" },
// "username": { "type": "keyword" },
// "nickname": { "type": "text" },
// "phone": { "type": "keyword" },
// "age": { "type": "integer" },
// "ancestral": { "type": "text" },
// "identity": { "type": "text" },
type testDocument struct {
	ID        int64  `json:"id"`
	Username  string `json:"username"`
	Nickname  string `json:"nickname"`
	Phone     string `json:"phone"`
	Age       int64  `json:"age"`
	Ancestral string `json:"ancestral"`
	Identity  string `json:"identity"`
}

func main() {
	// 创建ElasticSearch客户端
	client, err := elastic.NewClient(
		elastic.SetURL("http://162.14.70.254:9200"),
		elastic.SetSniff(false))
	if err != nil {
		panic(err)
	}
	// 输出client
	fmt.Println("hello world", client)

	// 创建文档
	doc := testDocument{
		ID:        1,
		Nickname:  "tomWang",
		Phone:     "123456",
		Age:       18,
		Ancestral: "China",
		Identity:  "English",
	}
	index, err := client.Index().
		Index("your_index").
		OpType("create").
		Id("1").
		BodyJson(doc).
		Do(context.Background())
	if err != nil {
		panic(err)
		return
	}
	fmt.Printf("indexed user %s to index %s, status %d, type %s\n", index.Id, index.Index, index.Status, index.Type)
}
